<template>
  <div class="pagination-container">
    <el-pagination
      v-model:currentPage="currentPageIn"
      v-model:page-size="pageSizeIn"
      :total="total"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :small="small"
      :disabled="disabled"
      :background="background"
      :layout="layout"
      :hideOnSinglePage="hideOnSinglePage"
      :popperClass="popperClass"
      :prevText="prevText"
      :nextText="nextText"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      v-bind="$attrs"
    >
    </el-pagination>
  </div>
</template>

<script>
import { PAGINATION_MAP } from '@/config'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: PAGINATION_MAP.pageSize
    },
    pageSizes: {
      type: Array,
      default() {
        return PAGINATION_MAP.pageSizes
      }
    },
    pagerCount: {
      type: Number,
      default: PAGINATION_MAP.pagerCount
    },
    layout: {
      type: String,
      default: PAGINATION_MAP.layout
    },
    background: {
      type: Boolean,
      default: PAGINATION_MAP.background
    },
    disabled: {
      type: Boolean,
      default: PAGINATION_MAP.disabled
    },
    small: {
      type: Boolean,
      default: PAGINATION_MAP.small
    },
    hideOnSinglePage: {
      type: Boolean,
      default: PAGINATION_MAP.hideOnSinglePage
    },
    popperClass: {
      type: String,
      default: PAGINATION_MAP.popperClass
    },
    prevText: {
      type: String,
      default: PAGINATION_MAP.prevText
    },
    nextText: {
      type: String,
      default: PAGINATION_MAP.nextText
    }
  },
  computed: {
    currentPageIn: {
      get() {
        return this.currentPage
      },
      set(val) {
        this.$emit('update:currentPage', val)
      }
    },
    pageSizeIn: {
      get() {
        return this.pageSize
      },
      set(val) {
        this.$emit('update:pageSize', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      /**
       * 场景：API返回总数为25条，按照10条每页，一共有3页。选了2的页数之后，然后把size选择成30条，
       * 这个时候，分页就会同时触发size选择函数以及current选择函数。{page: 2, size: 30}，{page: 1, size: 30}请求两次，会导致列表会有暂无数据的情况
       * 解决办法：用setTimeout(函数，0)，用延迟，虽然还是两次请求，但是每次都是{page: 1, size: 30}
       */
      // setTimeout(() => {
      //   this.$emit('pagination', { page: this.currentPage, limit: val })
      // }, 0)
      // this.$emit('pagination', { page: this.currentPage, limit: val })
      // console.log('handleSizeChange')
    },
    handleCurrentChange(val) {
      // this.$emit('pagination', { page: val, limit: this.pageSize })
      // console.log('handleCurrentChange')
    }
  }
}
</script>

<style lang="scss">
.pagination-container {
  margin-top: 15px;
  .el-pagination{
    justify-content: flex-end;
  }
}
</style>
